package gwt.immunology.client;

import gwt.immunology.client.mvp.AppActivityMapper;
import gwt.immunology.client.mvp.AppPlaceHistoryMapper;
import gwt.immunology.client.place.AllPatientsPlace;
import gwt.immunology.client.view.AppLayout;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;

public class Immunology implements EntryPoint {
    private Place defaultPlace = new AllPatientsPlace();
    private SimplePanel appWidget;
    private final AppLayout mainLayout = new AppLayout();

    public void onModuleLoad() {
        appWidget = mainLayout.getAppContentHolder();
        ClientFactory clientFactory = GWT.create(ClientFactory.class);
        EventBus eventBus = clientFactory.getEventBus();
        PlaceController placeController = clientFactory.getPlaceController();

        // Start ActivityManager for the main widget with our ActivityMapper
        ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
        ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
        activityManager.setDisplay(appWidget);

        // Start PlaceHistoryHandler with our PlaceHistoryMapper
        AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
        PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
        historyHandler.register(placeController, eventBus, defaultPlace);

        RootPanel.get().add(mainLayout);
        // Goes to the place represented on URL else default place
        historyHandler.handleCurrentHistory();
        History.newItem("all-patients");
    }
}
